Computer Programming: This Book Includes: Learn Python + SQL Programming + Arduino Programming by Parker Damon
Author:Parker, Damon [Parker, Damon]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2020-06-21T16:00:00+00:00
SELECT
pdctNam, sale
FROM
(SELECT
pdctCode,
ROUND(SUM(qtyOrdrd * pricEch)) sale
FROM
orderdetail
INNER JOIN order USING (orderNo)
WHERE
YEAR(shippdDte) = 2013
GROUP BY pdctCode
ORDER BY sale DESC
LIMIT 6) top6products2013
INNER JOIN
product USING (pdctCode);"
The query above will result in the output similar to the one shown in the picture below:
To drive this concept home, review a relatively complicated example of derived tables below:
Assume that the clients from the year 2019 have to be classified into three groups: platinum, gold and silver. Moreover, taking into consideration the criteria below, you would like to calculate the number of clients within each group:
• Platinum clients with orders larger than 99K in quantity.
• Gold clients with quantity orders ranging from 9K to 99K.
• Silver clients with orders less than 9K in quantity.
To generate a query for this analysis, you must first classify each client into appropriate group using CASE clause and GROUP BY function as shown in the syntax below:
SELECT
clientNo,
ROUND(SUM(qtyOrdrd * pricEch)) sale,
(CASE
WHEN SUM(qtyOrdrd * pricEch) < 9000 THEN 'Silver'
WHEN SUM(qtyOrdrd * pricEch) BETWEEN 9000 AND 99000 THEN 'Gold'
WHEN SUM(qtyOrdrd * pricEch) >99000 THEN 'Platinum'
END) clientGroup
FROM
orderdetail
INNER JOIN
orders USING (orderNo)
WHERE
YEAR(shippdDte) = 2019
GROUP BY clientNo;
The query output will be similar to the one shown in the picture below:
Now, you can execute the code below to generate a derived table and group the clients as needed:
SELECT
clientGroup,
COUNT(cg.clientGroup) AS grpCount
FROM
(SELECT
clientNo,
ROUND(SUM(qtyOrdrd * pricEch)) sales,
(CASE
WHEN SUM(qtyOrdrd * pricEch) <9000 THEN 'Silver' WHEN SUM(qtyOrdrd * pricEch) BETWEEN 9000 AND 99000 THEN 'Gold'
WHEN SUM(qtyOrdrd * pricEch) >99000 THEN 'Platinum'
END) clientGroup
FROM
orderdetail
INNER JOIN orders USING (orderNo)
WHERE
YEAR(shippdDte) = 2019
GROUP BY clientNo) cg
GROUP BY cg.clientGroup; "
The query output will be similar to the one shown in the picture below:
Chapter 3: Advanced SQL Functions
The most common command used in SQL is the SELECT command, which we have already used in the exercises mentioned earlier in this book. So, now let me give you a deep dive into the SQL SELECT command and various clauses that can be used with it.
MySQL SELECT
You can selectively fetch desired data from tables or views, using the SELECT statement. As you already know, similar to a spreadsheet, a table comprises rows and columns. You are highly likely to view a subset of columns, a subset of rows, or a combo of them both. The outcome of this query is known as a "result set,” which are lists of records comprising the same number of columns per record.
In the picture shown below of the employee table from the "MySQL sample database”, the table has 8 different columns and several records, as shown in the picture below:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8300)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6743)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6720)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6597)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6381)
Driving Data Quality with Data Contracts by Andrew Jones(6329)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(6093)
Learning SQL by Alan Beaulieu(5995)
Weapons of Math Destruction by Cathy O'Neil(5779)
Big Data Analysis with Python by Ivan Marin(5365)
Data Engineering with dbt by Roberto Zagni(4363)
Solidity Programming Essentials by Ritesh Modi(4010)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3872)
Pandas Cookbook by Theodore Petrou(3579)
Blockchain Basics by Daniel Drescher(3294)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2905)
Feature Store for Machine Learning by Jayanth Kumar M J(2814)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2796)
Mastering Python for Finance by Unknown(2744)
